package com.mmlc.widget;

import android.content.Context;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.mmlc.inter.ViewPagerAdapter;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.network.responsebean.GoodsDetailResponseBean;
import com.mmlc.utils.DataUtils;
import com.mmlc.utils.ViewUtils;
import com.mmlc.utils.XUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/8/6.
 */
public class ViewInfoBannerPager extends RelativeLayout {
    public ViewInfoBannerPager(Context context) {
        this(context, null);
    }

    public ViewInfoBannerPager(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    DisplayMetrics metrics;

    public ViewInfoBannerPager(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View.inflate(getContext(), R.layout.widget_banner_view, this);
        metrics = getResources().getDisplayMetrics();
        initView();
    }

    ViewPager vp_banner;
    LinearLayout ll_guide_point;

    private void initView() {
        vp_banner = ViewUtils.getView(this, R.id.vp_banner);
        ll_guide_point = ViewUtils.getView(this, R.id.ll_guide_point);

        datas = new ArrayList<>();
        views = new ArrayList<>();
    }

    List<GoodsDetailResponseBean.DataBean.GoodsAlbumBean> datas = new ArrayList<>();
    ArrayList<ImageView> views = new ArrayList<>();
    ViewPagerAdapter adapter;
    int bannerNum, currentNum, viewNum;

    public void setDatas(List<GoodsDetailResponseBean.DataBean.GoodsAlbumBean> datas) {
        this.datas.clear();
        this.datas.addAll(datas);

        displayBanner();
    }

    Handler bannerHandler = new Handler();
    final Runnable bannerRunnable = new Runnable() {

        @Override
        public void run() {
            currentNum++;

            if (isCycle && currentNum >= viewNum - 1) {
                currentNum = 1;
                vp_banner.setCurrentItem(currentNum, false);
            } else if (isCycle && currentNum <= 0) {
                currentNum = bannerNum;
                vp_banner.setCurrentItem(currentNum, false);
            } else {
                vp_banner.setCurrentItem(currentNum, true);
            }

            bannerHandler.postDelayed(bannerRunnable, 3000);
        }
    };

    boolean isCycle = true, isWheel = true;

    /**
     * TODO home banner
     */
    private void displayBanner() {
        views.clear();
        if (datas.size() < 2) {
            isCycle = false;
            isWheel = false;
        }

        bannerNum = datas.size();
        currentNum = 0;

        for (int i = 0; i < datas.size(); i++) {
            ImageView iv = new ImageView(getContext());
            setPagerParams(i, iv);
            views.add(iv);
        }
        if (bannerNum > 1) {
            ll_guide_point.removeAllViews();
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    18 * metrics.widthPixels / 1080,
                    18 * metrics.widthPixels / 1080);

            for (int i = 0; i < bannerNum; i++) {
                ImageView point = new ImageView(getContext());
                point.setLayoutParams(params);
                if (i == 0) {
                    point.setImageResource(R.drawable.shape_circle_black);
                    params.setMargins(18 * metrics.widthPixels / 1080, 0, 0, 0);
                } else {
                    point.setImageResource(R.drawable.shape_circle_grey);
                }
                ll_guide_point.addView(point);
            }
        }

        if (isCycle) {
            ImageView iv_last = new ImageView(getContext());
            setPagerParams(datas.size() - 1, iv_last);
            views.add(0, iv_last);

            ImageView iv_first = new ImageView(getContext());
            setPagerParams(0, iv_first);
            views.add(iv_first);

            currentNum = 1;
        }
        viewNum = views.size();

        adapter = new ViewPagerAdapter<>(views);
        vp_banner.setAdapter(adapter);
        vp_banner.setOffscreenPageLimit(3);
        vp_banner.setOnPageChangeListener(bannerPageChangeListener);
        vp_banner.setCurrentItem(currentNum, false);

        if (isWheel) {
            if (bannerHandler != null) {
                bannerHandler.removeCallbacksAndMessages(null);
            }
            bannerHandler.postDelayed(bannerRunnable, 2500);
        }
    }

    ViewPager.OnPageChangeListener bannerPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            if (bannerNum > 1) {
                currentNum = position;

                if (isCycle) {
                    if (currentNum == viewNum - 1) {
                        currentNum = 1;
                    } else if (currentNum == 0) {
                        currentNum = bannerNum;
                    }
                    vp_banner.setCurrentItem(currentNum, false);
                }

                if (bannerHandler != null) {
                    bannerHandler.removeCallbacks(bannerRunnable);
                }
                bannerHandler.postDelayed(bannerRunnable, 2500);

                setGuidePoint();
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            if (state == ViewPager.SCROLL_STATE_DRAGGING) {
            }
        }
    };

    private void setGuidePoint() {
        for (int i = 0; i < ll_guide_point.getChildCount(); i++) {
            ImageView point = (ImageView) ll_guide_point.getChildAt(i);
            if (i == currentNum - 1) {
                point.setImageResource(R.drawable.shape_circle_black);
            } else {
                point.setImageResource(R.drawable.shape_circle_grey);
            }
        }
    }

    private void setPagerParams(int i, ImageView iv) {
        final ArrayList<String> list = new ArrayList<>();
        for (int j = 0; j < datas.size(); j++) {
            list.add(datas.get(j).getImg());
        }
        iv.setTag(R.id.tag_one, i);
        XUtils.image(iv, datas.get(i).getImg(), XUtils.getImageOptionsBuilder().setImageScaleType(ImageView.ScaleType.CENTER_CROP).build());
        iv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                int index = (int) view.getTag(R.id.tag_one);
                DataUtils.toJump(getContext(),index,list);
            }
        });
    }
}
